﻿namespace CN2P.Web.Routing
{
    using System.Web.Http;
    using System.Web.Routing;
    using CN2P.Web.Controllers.Api;

    public static class RoutesExt
    {
        public static void MapHttpRoute<T>(this HttpRouteCollection routes, string name, string url, object defaults = null, object constraints = null, string httpVerb = null) where T : ApiController
        {
            var rd = new RouteValueDictionary(defaults);
            var type = typeof(T);
            rd["controller"] = type.Name.Replace("Controller", string.Empty);
            if (!string.IsNullOrWhiteSpace(httpVerb))
            {
                var crd = new RouteValueDictionary(constraints);
                crd["httpMethod"] = new HttpMethodConstraint(httpVerb);
                constraints = crd;
            }

            routes.MapHttpRoute(name, "api/" + url, rd, constraints);
        }

        public static void MapHttpRouteWithProxy<TImpl, TProxy>(this HttpRouteCollection routes, string name, string url, object defaults = null, object constraints = null, string httpVerb = null)
            where TImpl : BaseApiController
            where TProxy : BaseApiProxyController
        {
            routes.MapHttpRoute<TImpl>(name, "impl/" + url, defaults, constraints, httpVerb);
            routes.MapHttpRoute<TProxy>("proxy." + name, "proxy/" + url, defaults, constraints, httpVerb);
        }
    }
}